#!/bin/bash

# default: zabbix-agent on el7.x86_64
ZABBIX_AGENT_SVC="zabbix-agent"
ZABBIX_AGENT_PKG="zabbix-agent"
ZABBIX_AGENT_CONF="/etc/zabbix/zabbix_agentd.conf"

ZABBIX_GET_PKG="zabbix-get"

# https://repo.zabbix.com/zabbix/5.4/rhel/7/x86_64/zabbix-release-5.4-1.el7.noarch.rpm
ZABBIX_RELEASE_el7="zabbix-release-5.4-1.el7.noarch.rpm"
# https://repo.zabbix.com/zabbix/5.4/rhel/8/x86_64/zabbix-release-5.4-1.el8.noarch.rpm
ZABBIX_RELEASE_el8="zabbix-release-5.4-1.el8.noarch.rpm"
EPEL_RELEASE_el7="https://mirrors.tuna.tsinghua.edu.cn/epel/7/aarch64/Packages/e/epel-release-7-12.noarch.rpm"
EPEL_RELEASE_el8="https://mirrors.tuna.tsinghua.edu.cn/epel/8/Everything/aarch64/Packages/e/epel-release-8-13.el8.noarch.rpm"

ZABBIX_RELEASE="zabbix-release"
ZABBIX_RELEASE_REPO=$ZABBIX_RELEASE_el7

get_servers() {
    IFS=$','
    for x in $(cat $ZABBIX_AGENT_CONF |grep -w "^Server"| sed -e "s/^Server[[:space:]]*=[[:space:]]*//" -e "s/[[:space:]]*$//");do
        # e.g. 192.168.1.0/24 -> 192.168.1.0\/24
        [ "$x" == "127.0.0.1" ] || echo -n "${x//\//\\/},"
    done
    unset IFS
    # allow 127.0.0.1
    echo "127.0.0.1"
}

guess_zabbix_repo_yum_x86_64() {
    uname -a |grep -E "\.el7[_.]" &>/dev/null
    if [ $? -eq 0 ]; then
        ZABBIX_RELEASE="zabbix-release"
        ZABBIX_RELEASE_REPO=$ZABBIX_RELEASE_el7
    else
    uname -a |grep -E "\.el8[_.]" &>/dev/null
    if [ $? -eq 0 ]; then
        ZABBIX_RELEASE="zabbix-release"
        ZABBIX_RELEASE_REPO=$ZABBIX_RELEASE_el8
    else
        echo "error: os release not supported: $(uname -a)" >&1
        exit 2
    fi
    fi
}

guess_zabbix_repo_yum_aarch64() {
    uname -a |grep -w el7 &>/dev/null
    if [ $? -eq 0 ]; then
        ZABBIX_RELEASE="epel-release"
        ZABBIX_RELEASE_REPO=$EPEL_RELEASE_el7
    else
    uname -a |grep -w el8 &>/dev/null
    if [ $? -eq 0 ]; then
        ZABBIX_RELEASE="epel-release"
        ZABBIX_RELEASE_REPO=$EPEL_RELEASE_el8
    else
        echo "error: os release not supported: $(uname -a)" >&1
        exit 2
    fi
    fi
}


guess_zabbix_repo_apt_x86_64() {
    :
}

guess_zabbix_repo_apt_aarch64() {
    :
}

guess_zabbix_agent_yum_x86_64() {
    ZABBIX_AGENT_CONF="/etc/zabbix/zabbix_agentd.conf"
    ZABBIX_AGENT_SVC="zabbix-agent"
    ZABBIX_AGENT_PKG="zabbix-agent"
    rpm -qa |grep -w "^zabbix-agent" &>/dev/null
    if [ $? -ne 0 ]; then
    rpm -qa |grep -w "^zabbix-agent2" &>/dev/null
    if [ $? -eq 0 ]; then
        ZABBIX_AGENT_PKG="zabbix-agent2"
        ZABBIX_AGENT_CONF="/etc/zabbix/zabbix_agent2.conf"
        ZABBIX_AGENT_SVC="zabbix-agent2"
    fi
    fi
}

guess_zabbix_agent_yum_aarch64() {
    ZABBIX_AGENT_PKG="zabbix40-agent"
    ZABBIX_AGENT_CONF="/etc/zabbix_agentd.conf"
    ZABBIX_AGENT_SVC="zabbix-agent"
}

guess_zabbix_agent_apt_x86_64() {
    ZABBIX_AGENT_PKG="zabbix-agent"
    ZABBIX_AGENT_CONF="/etc/zabbix/zabbix_agentd.conf"
    ZABBIX_AGENT_SVC="zabbix-agent"
}

guess_zabbix_agent_apt_aarch64() {
    ZABBIX_AGENT_PKG="zabbix-agent"
    ZABBIX_AGENT_CONF="/etc/zabbix/zabbix_agentd.conf"
    ZABBIX_AGENT_SVC="zabbix-agent"
}

guess_zabbix_get_yum_x86_64() {
    ZABBIX_GET_PKG="zabbix-get"
}


guess_zabbix_get_yum_aarch64() {
    # zabbix_get included
    ZABBIX_GET_PKG="zabbix40"
}


guess_zabbix_get_apt_x86_64() {
    # it's small and zabbix_get included in.
    ZABBIX_GET_PKG="zabbix-proxy-sqlite3"
}
 

guess_zabbix_get_apt_aarch64() {
    ZABBIX_GET_PKG="zabbix-proxy-sqlite3"
}

unset pkgman
arch=$(uname -m)

type apt &>/dev/null && pkgman=apt || { type yum &>/dev/null && pkgman=yum; }
[ -n "$pkgman" ] || exit 255 

guess_zabbix_repo_${pkgman}_${arch}
guess_zabbix_agent_${pkgman}_${arch}
guess_zabbix_get_${pkgman}_${arch}

[ "$pkgman" == "yum" ] && { rpm -q $ZABBIX_RELEASE || yum install -y $ZABBIX_RELEASE_REPO; sed -i -e '/^sslverify/d' -e '/^\[zabbix.*/a sslverify=0' /etc/yum.repos.d/zabbix.repo; }
systemctl list-unit-files |grep -w $ZABBIX_AGENT_SVC || ${pkgman} install -y $ZABBIX_AGENT_PKG || exit 2
type zabbix_get || ${pkgman} install -y $ZABBIX_GET_PKG || exit 2

sed -i -e "s/^Server[[:space:]]*=.*/Server=$(get_servers)/" $ZABBIX_AGENT_CONF

systemctl enable $ZABBIX_AGENT_SVC
systemctl restart $ZABBIX_AGENT_SVC


cat >/usr/bin/zabbix-stat << EOF
#!/bin/bash
if=$(ip route |grep -w "^default" |head -n 1 |sed 's/^.* dev //' |awk '{print $1}')
[ -n "\$if" ] || if=$(ip -4 address show scope global up |grep "state UP" |head -n 1 |awk '{print $2}' |tr -d ':')

cpu=\$(zabbix_get -s 127.0.0.1 -k system.cpu.util) || { echo "{}"; exit 1; }
memory=\$(zabbix_get -s 127.0.0.1 -k vm.memory.size['used']) || { echo "{}"; exit 2; }
diskread=\$(zabbix_get -s 127.0.0.1 -k vfs.dev.read[,sps]) || { echo "{}"; exit 3; }
diskwrite=\$(zabbix_get -s 127.0.0.1 -k vfs.dev.write[,sps]) || { echo "{}"; exit 4; }
netin=\$(zabbix_get -s 127.0.0.1 -k net.if.in[\$if]) || { echo "{}"; exit 5; }
netout=\$(zabbix_get -s 127.0.0.1 -k net.if.out[\$if]) || { echo "{}"; exit 6; }

echo "{\"cpu_util\":{\"value\":\$cpu,\"unit\":\"%\"},\"mem_used\":{\"value\":\$memory,\"unit\":\"bytes\"},\"disk_read\":{\"value\":\$diskread,\"unit\":\"sectors/s\"},\"disk_write\":{\"value\":\$diskwrite,\"unit\":\"sectors/s\"},\"net_in\":{\"value\":\$netin,\"unit\":\"bytes\",\"if\":\"\$if\"},\"net_out\":{\"value\":\$netout,\"unit\":\"bytes\",\"if\":\"\$if\"}}"
EOF

chmod +x /usr/bin/zabbix-stat

echo "# zabbix-stat"
zabbix-stat

